package com.beta.admin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.admin.support.web.BaseController;
import com.beta.admin.vo.OrdersPayStatusVo;
import com.beta.admin.vo.OrdersStatusVo;
import com.beta.common.web.ContentType;
import com.beta.dto.order.BackendOrderDto;
import com.beta.dto.order.OrderSearchDto;
import com.beta.em.order.OrderStatus;
import com.beta.em.order.PayStatus;
import com.beta.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lichao on 2017/1/20.
 */
@RestController
@RequestMapping(value = RequestMappingConstants.MAPPING_PREFIX + "/orders")
public class OrderController extends BaseController {

    @Autowired
    private IOrdersService ordersService;

    @GetMapping(value = "", produces = ContentType.JSON)
    public Object getOrders(@RequestParam("code")String code,@RequestParam("payStatus")String payStatus,
                            @RequestParam("name")String name,@RequestParam("cityCode")String cityCode,
                            @RequestParam("orderTime1")String orderTime1,@RequestParam("orderTime2")String orderTime2,
                            @RequestParam("status")String status){
        OrderSearchDto orderSearchDto=new OrderSearchDto();
        orderSearchDto.setCode(code);
        orderSearchDto.setPayStatus(payStatus);
        orderSearchDto.setName(name);
        orderSearchDto.setCityCode(cityCode);
        orderSearchDto.setOrderTime1(orderTime1);
        orderSearchDto.setOrderTime2(orderTime2);
        orderSearchDto.setStatus(status);
        Page<BackendOrderDto> backendOrders = ordersService.getBackendOrders(getMybatisPage(),orderSearchDto);
        return backendOrders;
    }

    @GetMapping(value="/pay_status",produces = ContentType.JSON)
    public Object getOrderPayStatus(){
        PayStatus[] values = PayStatus.values();
        List<OrdersPayStatusVo> ordersPayStatusList=new ArrayList<OrdersPayStatusVo>();
        for (PayStatus payStatus:values) {
            OrdersPayStatusVo ordersPayStatus=new OrdersPayStatusVo();
            ordersPayStatus.setPayStatusValue(payStatus.getValue());
            ordersPayStatus.setPayStatusText(payStatus.getCnValue());
            ordersPayStatusList.add(ordersPayStatus);
        }
        return ordersPayStatusList;
    }

    @GetMapping(value = "/status",produces=ContentType.JSON)
    public Object getOrderStatus(){
        OrderStatus[] values = OrderStatus.values();
        List<OrdersStatusVo> list=new ArrayList <OrdersStatusVo>();
        for (OrderStatus orderStatus:values) {
            OrdersStatusVo ordersStatusVo=new OrdersStatusVo();
            ordersStatusVo.setOrderStatus(orderStatus.getValue());
            ordersStatusVo.setOrderStatusText(orderStatus.getCnValue());
            list.add(ordersStatusVo);
        }
        return list;
    }

    @GetMapping(value = "/{id}",produces = ContentType.JSON)
    public Object getOrderDetail(@PathVariable("id")Integer orderId){
        return ordersService.getOrderDetail(orderId);
    }
}
